package com.fr.web.core.service;

import com.fr.base.ColumnRow;
import com.fr.base.core.html.Tag;
import com.fr.report.FineBook;
import com.fr.report.Report;
import com.fr.report.ReportSettings;
import com.fr.report.ReportWebAttr;
import com.fr.report.SheetPage;
import com.fr.report.core.ReportUtils;
import com.fr.web.Browser;
import com.fr.web.ErrorHandler;
import com.fr.web.Repository;
import com.fr.web.core.ErrorHandlerHelper;
import com.fr.web.core.Service;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.TreeHTMLConverter;
import com.fr.web.core.WebUtils;
import com.fr.web.core.chwriter.FormCellWriter;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FormService
  implements Service
{
  private static FormService FORM_SERVICE = new FormService();

  public static FormService getInstance()
  {
    return FORM_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("form_");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString1, String paramString2)
    throws Exception
  {
    if ("form_content".equals(paramString1))
      readFormContent(paramString1, paramString2, paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void readFormContent(String paramString1, String paramString2, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    SessionIDInfor localSessionIDInfor = SessionDealWith.getSessionIDInfor(paramString2);
    if (localSessionIDInfor == null)
    {
      ErrorHandlerHelper.getErrorHandler().error(paramHttpServletRequest, paramHttpServletResponse, "SessionID: \"" + paramString2 + "\" time out.");
      return;
    }
    int i = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "reportIndex"));
    Report localReport = localSessionIDInfor.getReport2Show(i);
    SheetPage localSheetPage = new SheetPage(localReport);
    ColumnRow localColumnRow = null;
    if ((localReport.getReportSettings() != null) && (localReport.getReportSettings().getWriteFrozenColumnRow() != null))
      localColumnRow = localReport.getReportSettings().getWriteFrozenColumnRow();
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    Tag localTag1 = new Tag("div").cls("formContentDIV");
    Repository localRepository = new Repository(paramHttpServletRequest, localSessionIDInfor);
    ReportWebAttr localReportWebAttr = localRepository.getSessionIDInfor().getContextBook().getReportWebAttr();
    if (ReportUtils.isWebPageModel(localReportWebAttr))
      localTag1.cls("contentDIV").css("width", localSheetPage.getPageWidth() + "px").css("height", localSheetPage.getPageHeight() + "px");
    new TreeHTMLConverter(localSheetPage, new FormCellWriter(localRepository, i), localColumnRow, localRepository, localTag1, true).convert();
    if (localRepository.getBrowser().shouldWrapCenter())
    {
      localTag2 = localTag1;
      localTag1 = new Tag("center").sub(localTag2);
    }
    Tag localTag2 = new Tag("form").attr("name", "form-" + localSessionIDInfor.getReportName(i)).cls("form-submit");
    localTag1.sub(localTag2);
    localTag1.writeHtml(localPrintWriter);
    localPrintWriter.flush();
    localPrintWriter.close();
  }
}